#include "playscene.h"
#include<QDebug>
#include<QMenuBar>
#include<QPainter>
#include "mypushbutton.h"
#include<QTimer>
#include<QLabel>
#include "mycoin.h"
#include "dataconfig.h"
#include<QPropertyAnimation>
#include<QSound>
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{

//}

PlayScene::PlayScene(int index) {
//    qDebug() << "打开了" << index << "关";

    this->levelIndex = index;

    //设置固定大小
    this->setFixedSize(320, 588);

    //标题
    this->setWindowTitle("翻金币游戏");

    //图片
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //创建菜单栏
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);

    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");

    //创建菜单项
    QAction * quitAction = startMenu->addAction("退出");

    //点击退出 实现退游戏
    connect(quitAction, &QAction::triggered, [=](){
       this->close();
    });

    //默认未胜利
    this->isWin = false;

    QSound * flipSound = new QSound(":/res/ConFlipSound.wav", this);
    QSound * winSound = new QSound(":/res/LevelWinSound.wav", this);
    QSound * backSound = new QSound(":/res/TapButtonSound.wav", this);

    //返回按钮创建
    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");

    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(), this->height()-backBtn->height());
    connect(backBtn, &MyPushButton::clicked, [=](){
        //隐藏自身,发送信号. 信号槽

        backSound->play();

        QTimer::singleShot(50, this, [=]() {
           emit this->chooseSceneBack();
        });
    });


    //显示具体关卡号
    QLabel * label = new QLabel;
    label->setParent(this);
    QString str = QString("Level : %1").arg(this->levelIndex);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);

    //让label使用font
    label->setFont(font);

    label->setText(str);
    label->setGeometry(QRect(30, this->height()-50, 120, 50));


    //初始化游戏中二维数组
    dataConfig data;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            this->gameArray[i][j] = data.mData[this->levelIndex][i][j];
        }
    }


    //创建胜利效果图片
    QLabel * winLabel = new QLabel;
    QPixmap pix;
    pix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(QRect(0,0,pix.width(), pix.height()));
    winLabel->setParent(this);
    winLabel->setPixmap(pix);
    winLabel->move(0.5*(this->width() - pix.width()), -pix.height());


    //创建金币背景图片
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
           //QLable显示图片
            QLabel * bg = new QLabel(this);
            QPixmap pix;
            pix.load(":/res/BoardNode.png");

            bg->setGeometry(0, 0, pix.width(), pix.height());
            bg->setPixmap(pix);
            bg->move(57+i*50, 200+j*50);


            QString str;
            if (this->gameArray[i][j] == 1) {
                str = ":/res/Coin0001.png";
            } else {
                str = ":/res/Coin0008.png";
            }

            //创建金币
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(57+i*50, 204+j*50);
            coinBtn[i][j] = coin;

            //记录基本位置和状态
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->gameArray[i][j];

            connect(coin, &MyCoin::clicked, [=](){

                //播放音效
                flipSound->play();

                //禁按钮,lock效果
                for (int i=0; i<4; i++) {
                    for (int j=0; j<4; j++) {
                        coinBtn[i][j]->isWin = true;
                    }
                }

                coin->changeFlag();
                gameArray[i][j] = gameArray[i][j] == 1 ? 0 : 1;

                QTimer::singleShot(300, this, [=]() {
                    //延时继续翻动周围
                    //右侧硬币
                    if (coin->posX + 1 <= 3) {
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY] == 1 ? 0 : 1;
                    }
                    //左侧硬币
                    if (coin->posX - 1 >= 0) {
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY] == 1 ? 0 : 1;
                    }
                    if (coin->posY + 1 <= 3) {
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX][coin->posY+1] == 1 ? 0 : 1;
                    }
                    if (coin->posY - 1 >= 0) {
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX][coin->posY-1] == 1 ? 0 : 1;
                    }

                    //解禁按钮
                    for (int i=0; i<4; i++) {
                        for (int j=0; j<4; j++) {
                            coinBtn[i][j]->isWin = false;
                        }
                    }

                    //检测胜利标识
                    this->isWin = true;
                    for (int i=0; i<4; i++) {
                        for (int j=0; j<4; j++) {
                            if (coinBtn[i][j]->flag == false) {
                                this->isWin = false;
                                break;
                            }
                        }
                    }

                    if (this->isWin) {
                        qDebug() << "游戏胜利";

                        //播放音效
                        winSound->play();

                        for (int i=0; i<4; i++) {
                            for (int j=0; j<4; j++) {
                                coinBtn[i][j]->isWin = true;
                            }
                        }

                        //胜利图片显示
                        QPropertyAnimation * animation = new QPropertyAnimation(winLabel, "geometry");
                        animation->setDuration(1000);
                        animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
                        animation->setEndValue(QRect(winLabel->x(), winLabel->y()+114, winLabel->width(), winLabel->height()));
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        animation->start();
                    }

                });

            });

        }
    }

}

//重写绘图事件
void PlayScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");

    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    //绘制标题
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
    painter.drawPixmap(10, 30, pix.width(), pix.height(), pix);
}
